var qzimgID = '1';
var quimgup = '1';
// var app = getApp();
// class UPIMG {
//上传图片
// file = 1;
// constructor() {
//   this.file = 1;
// }
var upImgs = function() {
  var that = this;
  var fileid = '';
  // this.setData({
  //   qzimgID: 123
  // })
  // 选择图片
  wx.chooseImage({
    
    count: 1,
    sizeType: ['compressed'],
    sourceType: ['album', 'camera'],
    success: function(res) {
      wx.showLoading({
        title: '上传中',
      })
      const filePath = res.tempFilePaths[0]

      // 上传图片
      const cloudPath = new Date().getTime() + filePath.match(/\.[^.]+?$/)[0]
      console.log(res);
      // console.log(cloudPath);
      // console.log(filePath);
      wx.cloud.uploadFile({
        cloudPath,
        filePath,
        success: res => {
          console.log('[上传文件] 成功：', res);
          qzimgID = res.fileID;
          // fileid = res.fileID;
          // file = res.fileID;
          // console.log(file);
          // that.callBack(res.fileID);
          that.getimgpath(res.fileID);
          // that.setData({
          //   qzimgID: res.fileID
          // });
          app.globalData.fileID = res.fileID
          app.globalData.cloudPath = cloudPath
          app.globalData.imagePath = filePath
        },
        fail: e => {
          console.error('[上传文件] 失败：', e)
          wx.showToast({
            icon: 'none',
            title: '上传失败',
          })
        },
        complete: () => {
          wx.hideLoading()
        }
      })
    },
    fail: e => {
      console.error(e)
    }
  })
  return fileid;
}
// callBack = function(res) {
//   file = res;
// }
var getimgpath = function(path) {
  // console.log(1)
  // console.log(path)
  var that = this;
  wx.cloud.getTempFileURL({
    fileList: [path],
    success: res => {
      // get temp file URL
      console.log(res.fileList[0].tempFileURL)
      return res.fileList[0].tempFileURL;
      // that.setData({
      //   quimgup: res.fileList[0].tempFileURL
      // });
    },
    fail: err => {
      // handle error
    }
  })
}
// }
module.exports = {
  //上传图片
  upImgs: upImgs,
  //得到图片路径
  getimgpath: getimgpath,
  qzimgID: qzimgID,
  quimgup: quimgup
}
// module.exports = {
//   UPIMG: UPIMG,
//   file: file
// };

// module.exports = UPIMG;